<?php

use yii\db\Migration;

class m000005_000005_create_card_sms extends Migration
{
    public function safeUp()
    {
        $this->createTable('{{%card_sms}}', [
            'id' => $this->primaryKey(),
            'card_id' => $this->integer()->notNull()->comment('卡编号'),
            'type' => $this->integer()->notNull()->comment('类型'),
            'time' => $this->integer()->notNull()->comment('时间'),
            'msg_id' => $this->string(32)->comment('消息流水号'),
            'content' => $this->string(1024)->notNull()->comment('内容'),
            'status' => $this->integer()->notNull()->comment('状态'),
            'info' => $this->string(32)->comment('状态信息'),
            'remark' => $this->text()->comment('备注'),
        ]);
        $this->createIndex('fk_card_sms_card1_idx', '{{%card_sms}}', ['card_id']);
        try {
            $this->addForeignKey('fk_card_sms_card1', '{{%card_sms}}', ['card_id'], '{{%card}}', ['id']);
        } catch (Exception $e) {
            Yii::error($e->getMessage());
        }
        $this->createIndex('card_sms_msg_id_idx', '{{%card_sms}}', ['msg_id'], true);
        $this->batchInsert('{{%key_map}}', ['t', 'k', 'v'], [
            ['card_sms_type', 1, '下行'],
            ['card_sms_type', 2, '上行'],
            ['card_sms_status', 1, '待发送'],
            ['card_sms_status', 2, '暂停发送'],
            ['card_sms_status', 3, '提交成功'],
            ['card_sms_status', 4, '提交失败'],
            ['card_sms_status', 5, '发送成功'],
            ['card_sms_status', 6, '发送失败'],
        ]);
    }

    public function down()
    {
        $this->delete('{{%key_map}}', ['t' => 'card_sms_status']);
        $this->delete('{{%key_map}}', ['t' => 'card_sms_type']);
        $this->dropTable('{{%card_sms}}');
    }
}
